package com.example.myapplication;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.KeyguardManager;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class Service_Activity extends Activity {


    private Button service_start,service_stop;

    Button buttonOn,buttonOff,screen_on,screen_off;
    private KeyguardManager.KeyguardLock m_keyguardLock = null;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    turnOnScreen();
                    break;
                case 2:

                    break;
            }
        }
    };
    DevicePolicyManager policyManager;
    ComponentName adminReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service);

        service_start = (Button) findViewById(R.id.service_start);
        service_stop = (Button) findViewById(R.id.service_stop);
        screen_on = (Button) findViewById(R.id.screen_on);
        screen_off = (Button) findViewById(R.id.screen_off);


        service_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent startIntent = new Intent(Service_Activity.this, MyService.class);
                startService(startIntent);
            }
        });

        service_stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent stopIntent = new Intent(Service_Activity.this, MyService.class);
                stopService(stopIntent);
            }
        });


        buttonOff = (Button) findViewById(R.id.button_off);
        adminReceiver = new ComponentName(Service_Activity.this, AlarmReceiver.class);
        buttonOff.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                boolean admin = policyManager.isAdminActive(adminReceiver);
                if (admin) {
                    policyManager.lockNow();//休眠
                    handler.sendEmptyMessageDelayed(1, 3000);
                } else {

                    Toast.makeText(Service_Activity.this, "没有设备管理权限",
                            Toast.LENGTH_LONG).show();
                }
            }
        });
        buttonOn = (Button) findViewById(R.id.button_on);
        buttonOn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                turnOnScreen();
            }
        });
        mPowerManager = (PowerManager) getSystemService(POWER_SERVICE);
        policyManager = (DevicePolicyManager) Service_Activity.this.getSystemService(Context.DEVICE_POLICY_SERVICE);

        Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, adminReceiver);
        intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "开启后就可以使用锁屏功能了...");//显示位置见图二

        startActivityForResult(intent, 0);


        screen_on.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                //AcquireWakeLock(1000 * 5);
                Log.i("screen", "---------------UnlockedScreen-------------");

                if (m_keyguardLock != null) {
                    m_keyguardLock.disableKeyguard();
                }
                else {
                    KeyguardManager mKeyguardManager = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
                    m_keyguardLock = mKeyguardManager.newKeyguardLock("");
                }

            }
        });

        screen_on.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                //AcquireWakeLock(1000 * 5);
                Log.i("screen", "---------------LockScreen-------------");


                if (m_keyguardLock != null) {
                    m_keyguardLock.reenableKeyguard();
                }
                else {
                    KeyguardManager mKeyguardManager = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
                    m_keyguardLock = mKeyguardManager.newKeyguardLock("");
                }

            }
        });
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }


    private PowerManager mPowerManager;
    private PowerManager.WakeLock mWakeLock;

    @SuppressLint("InvalidWakeLockTag")
    public void turnOnScreen() {
        // turn on screen
        Log.v("ProximityActivity", "ON!");
        mWakeLock = mPowerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "tag");
        Log.i("powermager","on csreen");
        mWakeLock.acquire();//唤醒
        mWakeLock.release();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        isOpen();
    }

    /**
     * 检测用户是否开启了超级管理员
     */
    private void isOpen() {
        if(policyManager.isAdminActive(adminReceiver)){//判断超级管理员是否激活

            Toast.makeText(Service_Activity.this,"设备已被激活",
                    Toast.LENGTH_LONG).show();

        }else{

            Toast.makeText(Service_Activity.this,"设备没有被激活",
                    Toast.LENGTH_LONG).show();

        }
    }


}
